/***
 * 根据菜单编码生成当前用户拥有的菜单按钮toolbar
 */
Ext.define('Pearlsen.ux.BToolbar', {
    extend: 'Ext.toolbar.Toolbar',
    alias: 'widget.btoolbar',
    menuCode:null,
    constructor: function(config) {
		if( Ext.isEmpty(config.menuCode) ){
			throw new Error("没有设置menuCode");
		}
    	this.callParent([config]);
    	if(config.defaults && config.defaults.scope ){
    		this.scope = config.defaults.scope;
    	}else{
    		this.scope = null;
    	}
    	
    	this._loadButtons();
    },
    _loadButtons:function(){
		 var query = new Query("MenuQueryHandler","_load_dataset");
		 query.setHandlerMethod("getPageButtons");		 
		 query.setURL('/query.asp');
		 query.addCondition("menuCode", this.menuCode);
		 query.setCompletedFun(this.afterloadButtons);
		 query.setCompletedFunScope(this);
		 query.load();
    },
    _getScopeFromHandler:function(handlerName){
    		var scope="";
    		var len = handlerName.lastIndexOf(".");
    		if(len>-1){
    			scope=handlerName.substring(0,len);
    		}else{
    			scope = handlerName;
    		}
    		return eval(scope);
    },
    afterloadButtons:function(success,dataset,response, opts){
		if(success){
			if( dataset && !Ext.isEmpty(dataset.record)){
				for(var i=0;i<dataset.record.length;i++){
					var rec = dataset.record[i];
					
					var menuCode = rec['MENU_CODE'];
					var menuName = rec['MENU_NAME'];
					var menuHandler = rec['MENU_HANDLER'];
					var menuCls = rec['MENU_IMG_CLS'];
					var button ={};
					button['id'] = menuCode;
					button['text'] = menuName;
					button['iconCls'] = menuCls;
					if( this.scope==null ){
						var scope =  this._getScopeFromHandler(rec['MENU_HANDLER']);
						button['scope'] = eval(scope);
					}
					if( !Ext.isEmpty(menuHandler) ){
						button['handler'] = eval(menuHandler);
					}		
					this.add(button);
					this.add("-");
				}
			}
		}else{
			throw new Error("获取buttons出现异常");
		}
    }
});